package com.restaurant.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.restaurant.domain.Menusize;
import com.restaurant.service.MenusizeService;
import com.restaurant.validators.MenuSizeFormValidator;

@Component
@RequestMapping("/action/menusize.do")
@SessionAttributes("menusize")
public class MenuSizeFormController {

	private MenusizeService objMenusizeService;
	private MenuSizeFormValidator menuSizeFormValidator;

	public MenusizeService getObjMenusizeService() {
		return objMenusizeService;
	}
	
	@Autowired
	public void setObjMenusizeService(MenusizeService objMenusizeService) {
		this.objMenusizeService = objMenusizeService;
	}
	
	public MenuSizeFormValidator getMenuSizeFormValidator() {
		return menuSizeFormValidator;
	}

	@Autowired
	public void setMenuSizeFormValidator(MenuSizeFormValidator menuSizeFormValidator) {
		this.menuSizeFormValidator = menuSizeFormValidator;
	}

	@RequestMapping(method=RequestMethod.GET)
	public String menuSizeGetMethodRequest(ModelMap map){
		Menusize menusize = new Menusize();
		ArrayList<Menusize> listMenuSize = null;
		
		try {
			listMenuSize = (ArrayList<Menusize>) objMenusizeService.loadByHql("from Menusize where isdeleted=false");
		} catch (Exception e) {
			listMenuSize = new ArrayList<Menusize>();
		}
		
		map.addAttribute("menusize", menusize);
		map.addAttribute("listMenuSize", listMenuSize);
		return "MenuSize";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String menuSizePostMethodRequest(ModelMap map, @ModelAttribute ArrayList<Menusize> listMenuSize ,@ModelAttribute Menusize menusize, BindingResult result, HttpServletRequest request, HttpServletResponse response){
		
		String action = request.getParameter("action");
		
		try{
			if(action.equals("addnew")){
				menuSizeFormValidator.validate(menusize, result);
				if(result.hasErrors()){
					try {
						listMenuSize = (ArrayList<Menusize>) objMenusizeService.loadByHql("from Menusize where isdeleted=false");
					} catch (Exception e) {
						e.printStackTrace();
					}
					map.addAttribute("listMenuSize", listMenuSize);
					return "MenuSize";
				}
				menusize.setIsdeleted(false);
				objMenusizeService.addOrModify(menusize);
				menusize = new Menusize();
			}else if(action.equals("edit")){
				menusize = objMenusizeService.loadByPrimaryKey(Long.parseLong(request.getParameter("id")));
				objMenusizeService.addOrModify(menusize);
			}else if(action.equals("del")){
				menusize = objMenusizeService.loadByPrimaryKey(Long.parseLong(request.getParameter("id")));
				menusize.setIsdeleted(true);
				objMenusizeService.addOrModify(menusize);
				menusize = new Menusize();
			}else{
				return "redirect:menusize.do";
			}
			
		}catch(Exception e){
			return "redirect:menusize.do";
		}
		
		try {
			listMenuSize = (ArrayList<Menusize>) objMenusizeService.loadByHql("from Menusize where isdeleted=false");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		map.addAttribute("menusize", menusize);
		map.addAttribute("listMenuSize", listMenuSize);
		return "MenuSize";
	}
}
